package com.archstoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.EngineesListEntity;
import com.base.utils.StringUtil;

import java.util.List;

/**
 * 工程实务适配器
 */
public class MainEngineerAdapter extends BaseAdapter {

    private Context mContext;
    private List<EngineesListEntity.DataBean> mData;

    public MainEngineerAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<EngineesListEntity.DataBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addData(List<EngineesListEntity.DataBean> data) {
        if (mData == null) {
            mData = data;
        } else {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public EngineesListEntity.DataBean getItem(int i) {
        if (mData == null || i >= mData.size()) {
            return null;
        } else {
            return mData.get(i);
        }
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_tender, null);
            holder.mTitleTv = (TextView) view.findViewById(R.id.item_title_tv);
            holder.mFromTv = (TextView) view.findViewById(R.id.item_from_tv);
            holder.mTimeTv = (TextView) view.findViewById(R.id.item_time_tv);
            holder.mStateTv = (TextView) view.findViewById(R.id.item_state_tv);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        holder.mTitleTv.setText(mData.get(i).getTitle());
        holder.mFromTv.setVisibility(StringUtil.isEmpty(mData.get(i).getReleaseOrg())
                ? View.GONE : View.VISIBLE);
        holder.mFromTv.setText(mData.get(i).getReleaseOrg());
        if (StringUtil.isEmpty(mData.get(i).getReleaseTime())
                && StringUtil.isEmpty(mData.get(i).getEffectiveTime())){
            holder.mTimeTv.setVisibility(View.GONE);
        } else if (StringUtil.isEmpty(mData.get(i).getReleaseTime())) {
            holder.mTimeTv.setVisibility(View.VISIBLE);
            holder.mTimeTv.setText( mData.get(i).getEffectiveTime()
                    + mContext.getString(R.string.item_effective_tv));
        } else if (StringUtil.isEmpty(mData.get(i).getEffectiveTime())) {
            holder.mTimeTv.setVisibility(View.VISIBLE);
            holder.mTimeTv.setText(mData.get(i).getReleaseTime()
                    + mContext.getString(R.string.item_release_tv));
        } else {
            holder.mTimeTv.setVisibility(View.VISIBLE);
            holder.mTimeTv.setText(mData.get(i).getReleaseTime()
                    + mContext.getString(R.string.item_release_tv) + "  "
                    + mData.get(i).getEffectiveTime()
                    + mContext.getString(R.string.item_effective_tv));
        }
        if (StringUtil.isEmpty(mData.get(i).getLrsStatus())) {
            holder.mStateTv.setVisibility(View.GONE);
        } else {
            holder.mStateTv.setVisibility(View.VISIBLE);
            holder.mStateTv.setText(mData.get(i).getLrsStatus());
        }

        return view;
    }

    class Holder {
        TextView mTitleTv, mFromTv, mTimeTv, mStateTv;
    }
}